package com.telit.common.service;

import com.telit.common.constant.FileTypeConstants;
import com.telit.common.constant.MsgConstants;
import com.telit.common.enums.ResultCode;
import com.telit.common.exception.ForestBaseException;
import com.telit.common.pojo.AliPushMessagePojo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;


/**
 * @Auther amy
 * @Describe aliyun短信推送基础数据校验
 */
@Service
public class ParamsCheckService {

    private static final List<String> PUSH_TYPE_LIST = Stream.of(MsgConstants.PUSH_TYPE_MESSAGE,MsgConstants.PUSH_TYPE_NOTICE).collect(toList());
    private static final List<String> TARGET_LIST = Stream.of(MsgConstants.TARGET_ACCOUNT,MsgConstants.TARGET_ALIAS,MsgConstants.TARGET_DEVICE,MsgConstants.TARGET_TAG,MsgConstants.TARGET_ALL).collect(toList());
    private static final List<String> DEVICE_TYPE_LIST = Stream.of(MsgConstants.DEVICE_TYPE_ANDROID,MsgConstants.DEVICE_TYPE_IOS,MsgConstants.DEVICE_TYPE_ALL).collect(toList());

    public static final List<String> FILE_TYPE_IMAGE_LIST = Stream.of(FileTypeConstants.FILE_TYPE_IMAGE_GIF,FileTypeConstants.FILE_TYPE_IMAGE_JPG,FileTypeConstants.FILE_TYPE_IMAGE_MAX_JPG,FileTypeConstants.FILE_TYPE_IMGAE_PNG,FileTypeConstants.FILE_TYPE_IMAGE_JPEG).collect(toList());
    public static final List<String> FILE_TYPE_DOC_LIST = Stream.of(FileTypeConstants.FILE_TYPE_DOC,FileTypeConstants.FILE_TYPE_DOC_DOCX,FileTypeConstants.FILE_TYPE_DOC_HTML,
                                                                        FileTypeConstants.FILE_TYPE_DOC_PDF,FileTypeConstants.FILE_TYPE_DOC_XLS,FileTypeConstants.FILE_TYPE_DOC_XLSX,FileTypeConstants.FILE_TYPE_DOC_ZIP).collect(toList());
    public static final List<String> FILE_TYPE_AUDIO_LIST = Stream.of(FileTypeConstants.FILE_TYPE_AUDIO_MP3,FileTypeConstants.FILE_TYPE_AUDIO_WAV,FileTypeConstants.FILE_TYPE_AUDIO_AAC,FileTypeConstants.FILE_TYPE_AUDIO_M4A,FileTypeConstants.FILE_TYPE_AUDIO_AMR).collect(toList());
    public static final List<String> FILE_TYPE_VIDEO_LIST = Stream.of(FileTypeConstants.FILE_TYPE_VIDEO_AVI,FileTypeConstants.FILE_TYPE_VIDEO_FLV,FileTypeConstants.FILE_TYPE_VIDEO_MP4,FileTypeConstants.FILE_TYPE_VIDEO_RMVB,FileTypeConstants.FILE_TYPE_VIDEO_WMV).collect(toList());

    public void checkPushParam(AliPushMessagePojo pojo){
        if(!PUSH_TYPE_LIST.contains(pojo.getPushType())){
           throw new ForestBaseException(ResultCode.FORM_PARAMS_CHECK_ERROR.getCode(),"pushType 参数不符合");
       }
       if(!DEVICE_TYPE_LIST.contains(pojo.getDeviceType())){
           throw new ForestBaseException(ResultCode.FORM_PARAMS_CHECK_ERROR.getCode(),"deviceType 参数不符合");
       }
        if(!TARGET_LIST.contains(pojo.getTarget())){
            throw new ForestBaseException(ResultCode.FORM_PARAMS_CHECK_ERROR.getCode(),"target 参数不符合");
        }
        if(!MsgConstants.TARGET_ALL.equals(pojo.getTarget()) && CollectionUtils.isEmpty(pojo.getAccountList())){
            throw new ForestBaseException(ResultCode.FORM_PARAMS_CHECK_ERROR.getCode(),"发送的目标对象不能为空");
        }
    }

    /**
     * 判断文件类型
     * @param fileType
     * @return
     */
    public String checkFieType(String fileType){
        String fileTypeSuffix = "other";
        if(FILE_TYPE_IMAGE_LIST.contains(fileType)){
            fileTypeSuffix = "image";
        }else if(FILE_TYPE_DOC_LIST.contains(fileType)){
            fileTypeSuffix = "document";
        }else if(FILE_TYPE_AUDIO_LIST.contains(fileType)){
            fileTypeSuffix = "audio";
        }else if(FILE_TYPE_VIDEO_LIST.contains(fileType)){
            fileTypeSuffix = "video";
        }
        return fileTypeSuffix;
    }
}
